﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using log4net;

namespace LIMS.BLL
{
    /// <summary>
    /// 公共分页，简要封装
    /// </summary>
    /// <typeparam name="TSource">数据源约束类型，具有公共无参构造方法</typeparam>
    public class CommonPager<TSource> where TSource : class
    {
        /// <summary>
        /// 获取指定类型、指定页的数据
        /// </summary>
        /// <param name="source">指定的数据源</param>
        /// <param name="pageSize">每页显示的数据项数</param>
        /// <param name="pageIndex">页码索引</param>
        /// <returns>返回分页后的数据</returns>
        public static IEnumerable<TSource> GetTSourceByPager(IEnumerable<TSource> source, Int32 pageSize, Int32 pageIndex)
        {
            return source.Skip(pageSize * (pageIndex - 1)).Take(pageSize);
        }
    }

    /// <summary>
    /// 全文检索：公共转换类
    /// </summary>
    public static class ConventerCommon
    {
        public static System.Data.DataTable LinqToDataTable<T>(IEnumerable<T> varList)
        {
            var dt = new System.Data.DataTable();
            var ps = typeof(T).GetProperties().ToList();
            ps.ForEach(p => { dt.Columns.Add(p.Name, p.PropertyType); });
            foreach (T item in varList)
            {
                var dr = dt.NewRow();
                var vs = ps.Select(p => p.GetValue(item, null));
                var ls = vs.ToList();
                int i = 0;
                ls.ForEach(c => dr[i++] = c);
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public static System.Data.DataTable ToDataTable<T>(IEnumerable<T> varList)
        {
            var dt = new System.Data.DataTable();
            var ps = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T));
            foreach (System.ComponentModel.PropertyDescriptor dp in ps)
            {
                dt.Columns.Add(dp.Name, dp.PropertyType);
            }
            foreach (T item in varList)
            {
                var dr = dt.NewRow();
                foreach (System.ComponentModel.PropertyDescriptor dp in ps)
                {
                    dr[dp.Name] = dp.GetValue(item);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public static System.Data.DataView FullTextSearch(this System.Data.DataTable dataTable, String[] keyWords)
        {
            var query = dataTable.
                    AsEnumerable().
                    Where<System.Data.DataRow>(row =>
                        keyWords.All(keyword =>
                            row.ItemArray.Select(
                                p => p.ToString()
                            ).
                            Any<String>(
                                s => s.Contains(keyword)
                            )
                        )
                    );
            //var query = dataTable.AsEnumerable().Where<DataRow>(
            //                                row => keyWords.All(keyword => row.ItemArray.Select(p => p.ToString()).Any<string>(s => s.Contains(keyword))));
            return query.AsDataView();
        }

    }

    /// <summary>
    /// 日志辅助类
    /// </summary>
    public class LogHelper
    {
        private static ILog log = null;
        private static LogHelper logHelper = null;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static ILog GetInstance()
        {
            logHelper = new LogHelper(null);
            return log;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="configPath"></param>
        /// <returns></returns>
        public static ILog GetInstance(String configPath)
        {
            logHelper = new LogHelper(configPath);
            return log;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configPath"></param>
        private LogHelper(String configPath)
        {
            if (!String.IsNullOrWhiteSpace(configPath))
            {
                log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath));
            }
            else
            {
                log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
            //ILog log = LogHelper.GetInstance();
            //log.Fatal("Fatal");
        }


    }
}
